const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //0可使用  2已失效
    couponData: [], //数据
    isHints: false, //无数据提示信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.getUserExchangeCoupon(this.data.type);
  },

  /**
   * 查询不同状态的券
   */
  selectStateCoupon: function (e) {
    var type = e.currentTarget.dataset.type; //页面类型
    this.setData({
      couponData: [],
      type: type,
      isHints: false
    }, function () {
      this.getUserExchangeCoupon(type);
    })
  },
  /**
   * 兑换券列表
   */
  getUserExchangeCoupon: function (type) {
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 8;
    param.assetStatus = type; //0可使用  2已失效
    memberModel.userExchangeCoupon(param).then(data => {
      var that = this;
      if (data && data.length > 0) {
        var dataList = [];
        for (var i = 0; i < data.length; i++) {
          var list = {};
          list.couponName = data[i].couponName;
          list.validEnd = data[i].validEnd ? data[i].validEnd.substr(0, 4) + "年" + data[i].validEnd.substr(5, 2) + "月" + data[i].validEnd.substr(8, 2) + "日" : "";
          list.coinCount = data[i].coinCount;
          list.assetStatus = data[i].assetStatus;
          list.couponCode = data[i].couponCode;
          dataList.push(list);
        }
        that.setData({
          isHints: false,
          couponData: dataList
        });
      } else {
        that.setData({
          couponData: [],
          isHints: true
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 跳转到兑换详情页面
   */
  goExchangeCouponDetails: function (event) {
    if (this.data.type == 0) {
      var name = event.currentTarget.dataset.name; //兑换券名称
      var code = event.currentTarget.dataset.code; //兑换券静态码
      var time = event.currentTarget.dataset.time; //有效期
      wx.navigateTo({
        url: '/pages/memberCenter/exchangeCouponDetails/index?name=' + name + '&code=' + code + '&time=' + time
      })
    }
  }
})